define(function(require, exports){
  const $ = require('jquery')
  const Handlebars = require('handlebars')
  const {
    customPieChart,
    customHistogram,
    customHistogramR,
    customearth,
  } = require('app/func')
  const earth = require('app/earth')

  let $topicList = $('#page-topic>.right')
  let $topicLeft = $('#page-topic>.left')
  let $backbtn = $('#page-topic>.back-btn')
  let $webglBgRound = $('#webgl-bg-round')

  let tpl_topicList = Handlebars.compile($('#topic-todayinfo').html())
  let tpl_topLeft = Handlebars.compile($('#tpl-hometopic').html())

  let inited = false

  $('#page-topic').addClass('fadeOut')
  
  exports.routeIn = async function(from){
    $('#back-btn').addClass('show')
    $('#page-topic').removeClass('fadeOut')
    $('#head-right-search').removeClass('center')
    earth.showDetailView()
    earth.$cont.addClass('fadeTop')
    $webglBgRound.addClass('fadeTop')
    renderTopicList()
    renderCharts()
    if(!inited){
      inited = true
      $backbtn.on('click', () => history.back())
    }
  }

  exports.routeOut = function(to) {
    $('#page-topic').addClass('fadeOut')
    earth.$cont.removeClass('fadeTop')
    $webglBgRound.removeClass('fadeTop')
    earth.hideDetailView()    
  }

  function renderTopicList(){
    let id = location.href.split('=')[1]
    $.post('api/topic/getTopic?id='+id,function(result){
      sessionStorage.topicStartTime=result.obj.topicStartTime
      sessionStorage.topicEndTime=result.obj.topicEndTime
      let conten = {
        title:result.obj.topicName,
        info:result.obj.topicSummary,
        topicPerson:result.obj.topicPerson.split(' '),
        topicAddress:result.obj.topicAddress.split(' '),
      }
      $('#page-topic>.title').html(result.obj.topicName) 
      $topicLeft.html(tpl_topLeft(conten))
    })
    let data = {
      startTime:$('#start').val(),
      endTime:$('#end').val(),
      type:$("input[name='search-lnav0']:checked").val(),
      wd:$('#head-right-search>input').val(),
      currentPage:1,
      page:5
    }
    $.post('api/topic/getTopicInfoList?currentPage=1&page=100&topicId='+id,data,function(result){
      let mock = {}
      mock.news=result.obj
      Handlebars.registerHelper('timePO',function(value){
        return tiemChange(value)
      })
      $topicList.html(tpl_topicList(mock))
    })
  }
  function renderCharts(){
    let id=location.href.split('=')[1]
    let dataset = []
    $.post('api/topic/getTopicInfoNumByPoTime?id='+id,function(result){
      dataset=[]
      let arr=[]
      $.each(result.obj,function(i,item){
        dataset.push(item.num)
        arr.push(i)
      })
      customHistogram('#page-topic>.charts .chart1', dataset,arr)
    })
    
    $.post('api/topic/getTopicInfoNumByCountry?id='+id,function(result){
    dataset=[]
    let daset={}
    $.each(result.obj,function(i,item){
        daset.label=item.country
        daset.count=item.num
        let daset2={label:daset.label,count:daset.count}
        dataset.push(daset2)
      })
    customPieChart('#page-topic>.charts .chart2', dataset)
  })
    $.post('api/topic/getTopicInfoNumByWebSite?startTime='+sessionStorage.topicStartTime+'&endTime='+sessionStorage.topicEndTime+'&id='+id,function(result){
      dataset=[]
      $.each(result.obj,function(i,item){
        let daArr=[]
        daArr.push(item.webSite)
        daArr.push(item.num)
        dataset.push(daArr)
      })
        customHistogramR('#page-topic>.charts .chart3', dataset)
    })
    $.post('api/topic/getTopicInfoNumByCountry?id='+id,function(result){
      let dataset1=[]
      $.each(result.obj,function(i,item){
        dataset1.push({'code':i,'name':item.country,"value":i+'3658',"color":'#eea638','num':item.num})
      })
      
      customearth('#page-topic>.center .chart4', dataset1)
      
    })
  
  }
})